package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;

    /**
     * 商品添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //这里还需要获取一下用户的ID
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //先查一下
        List<ShoppingCart> list = shoppingCartMapper.select(shoppingCart);
        //如果表为空，说明首次添加
        if (list==null|| list.isEmpty()) {
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId == null) {
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
            } else {
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insert(shoppingCart);
        }else {
            //否则说明已经有数据了，把它的份数加一 #{number}
            ShoppingCart sc = list.get(0);
            sc.setNumber(sc.getNumber() + 1);
            shoppingCartMapper.update(sc);
        }

    }

    /**
     * 移除商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(userId);

        //先查一下
        List<ShoppingCart> list = shoppingCartMapper.select(shoppingCart);
        //如果表为空，说明首次添加
        if (list==null|| list.isEmpty()) {
            log.error("购物车中没有此商品");
        }else{
            Integer number = list.get(0).getNumber();
            if(number == 1){
                shoppingCartMapper.delete(shoppingCart);
            }else if(number >1){
                shoppingCart = list.get(0);
                shoppingCart.setNumber(number-1);
                shoppingCartMapper.update(shoppingCart);
            }

        }

    }

    /**
     * 查看购物车商品
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.getByUserId(userId);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.cleanByUserId(BaseContext.getCurrentId());
    }

}
